<template>
  <div class="app-container">
    <el-card class="filter-container" shadow="never">
      <div>
        <i class="el-icon-search" />
        <span style="font-size: 20px">筛选搜索</span>
      </div>
      <div style="margin-top: 15px">
        <el-form :inline="true" :model="listQuery" size="small" label-width="150px">
          <el-form-item label="日期选择">
            <el-date-picker
              v-model="datePicker"
              type="daterange"
              unlink-panels
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :picker-options="pickerOptions"
              value-format="yyyy-MM-dd"
              @change="datePickerChange"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="fetchData">查询</el-button>
          </el-form-item>
          <el-form-item>
            <el-button @click="handleResetDatePicker">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
      :default-sort="{prop: 'oderTime', order: 'descending'}"
    >
      <el-table-column prop="id" align="center" label="订单号" width="95">
        <template slot-scope="scope">{{ scope.row.orderEntity.id }}</template>
      </el-table-column>
      <el-table-column prop="userId" align="center" label="用户id" width="95">
        <template slot-scope="scope">{{ scope.row.orderEntity.userId }}</template>
      </el-table-column>
      <el-table-column prop="dishes" align="center" label="菜品">
        <template slot-scope="scope">
          <!-- {{ scope.row.list }} -->
          <p v-for="(item, index) in scope.row.list" :key="index">{{ item }}</p>
        </template>
      </el-table-column>
      <el-table-column prop="totalAmount" align="center" label="总金额" width="100">
        <template slot-scope="scope">{{ scope.row.orderEntity.totalAmount }}</template>
      </el-table-column>
      <el-table-column
        prop="status"
        class-name="status-col"
        label="Status"
        width="110"
        align="center"
      >
        <template slot-scope="scope">
          <el-tag :type="scope.row.orderEntity.state | stateFilter">{{ scope.row.orderEntity.state | stateWordFilter }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="oderTime" align="center" label="oderTime" width="200" sortable>
        <template slot-scope="{ row }">
          <i class="el-icon-time" />
          <span>{{ row.orderEntity.createTime | timeFilter }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" width="100">
        <template slot-scope="{row,$index}">
          <el-button
            v-if="row.orderEntity.state === 0"
            size="mini"
            type="primary"
            @click="handleTakeOrder(row.orderEntity, $index)"
          >
            <i class="el-icon-edit" />接单
          </el-button>
          <el-button
            v-if="row.orderEntity.state === 1"
            size="mini"
            type="success"
            @click="handleFindshOrder(row.orderEntity, $index)"
          >
            <i class="el-icon-edit" />完成
          </el-button>
          <!-- <el-button v-if="row.status == 2" size="mini" type="danger" @click="handleDeleteOrder(row, $index)">
            <i class="el-icon-edit" />删除
          </el-button>-->
        </template>
      </el-table-column>
    </el-table>
    <div class="block" style="float: right;margin-top: 15px">
      <el-pagination
        background
        layout="total, sizes,prev, pager, next,jumper"
        :page-size="listQuery.pageSize"
        :page-sizes="[5,10,15]"
        :current-page.sync="listQuery.pageNum"
        :total="totalNum"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script>
import { getOrder, updateOrder } from '@/api/order'
import { parseTime } from '@/utils/index'

export default {
  filters: {
    stateFilter(state) {
      const stateMap = {
        0: 'danger',
        1: 'primary',
        2: 'success',
        3: 'info',
        4: 'info'
      }
      return stateMap[state]
    },
    stateWordFilter(state) {
      const stateWordMap = {
        0: '待确认',
        1: '准备中',
        2: '完成',
        3: '取消',
        4: '已评价'
      }
      return stateWordMap[state]
    },
    timeFilter(time) {
      return parseTime(time)
    }
  },
  data() {
    return {
      list: null,
      listLoading: true,
      listQuery: {
        pageNum: 1,
        pageSize: 5,
        datePickerStart: null,
        datePickerEnd: null
      },
      totalNum: 0,
      datePicker: '',
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
            picker.$emit('pick', [start, end])
          }
        }]
      }
    }
  },
  created() {
    this.fetchData() // 全部加载
  },
  methods: {
    fetchData() {
      this.listLoading = true
      getOrder(this.listQuery).then(response => {
        this.listLoading = false
        this.list = response.data.content
        this.totalNum = response.data.total
      })
    },
    datePickerChange() {
      if (this.datePicker !== null) {
        this.listQuery.datePickerStart = this.datePicker[0]
        this.listQuery.datePickerEnd = this.datePicker[1]
      } else {
        this.listQuery.datePickerStart = null
        this.listQuery.datePickerEnd = null
      }
    },
    handleResetDatePicker() {
      this.listQuery.datePickerStart = null
      this.listQuery.datePickerEnd = null
      this.datePicker = null
    },
    handleTakeOrder(order, index) {
      updateOrder(order.id, 'handling').then(res => {
        this.$message.success('接单成功')
        this.fetchData()
      })
    },
    handleFindshOrder(order, index) {
      updateOrder(order.id, 'finished').then(res => {
        this.$message.success('订单完成')
        this.fetchData()
      })
    },
    handleDeleteOrder(order, index) {
      // console.info(order)
      this.$message('删除订单')
    },
    handleSizeChange(val) {
      this.listQuery.pageNum = 1
      this.listQuery.pageSize = val
      this.fetchData()
    },
    handleCurrentChange(val) {
      this.listQuery.pageNum = val
      this.fetchData()
    }
  }
}
</script>

<style scoped>
.line {
  text-align: center;
}
</style>
